package gson.emlog.adapter;

import gson.emlog.R;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TwitterAdapter extends BaseAdapter {
	private JSONArray items;
	private Context mContext;
	
	public TwitterAdapter(JSONArray data, Context mContext) {
		super();
		this.items = data;
		this.mContext = mContext;
	}

	public int getCount() {
		return items.size();
	}

	public JSONObject getItem(int position) {
		try {
			return (JSONObject) items.get(position);
		} catch (Exception e) {
			return null;
		}
	}

	public long getItemId(int position) {
		try {
			JSONObject obj = this.getItem(position);
			if (obj != null) {
				return Integer.parseInt(obj.get("id").toString());
			}
		} catch (Exception e) {
			return position;
		}
		return position;
	}
	
	public int getReplyCount(int position){
		try {
			JSONObject obj = this.getItem(position);
			if (obj != null) {
				return (Integer) obj.get("rn");
			}
		} catch (Exception e) {
			return position;
		}
		return -1;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		//获取layout实例对象
		LayoutInflater lif = LayoutInflater.from(mContext);
		LinearLayout item = (LinearLayout) lif.inflate(R.layout.twitteritem, null);
		if(item != null){
			try {
				JSONObject data = (JSONObject) items.get(position);
				//赋值
				TextView  twpepole = (TextView) item.findViewById(R.id.twpepole);
				twpepole.setText(data.get("n").toString());

				TextView  twdate = (TextView) item.findViewById(R.id.twdate);
				twdate.setText(data.get("d").toString());
				
				TextView  twct = (TextView) item.findViewById(R.id.twct);
				twct.setText(data.get("ct").toString());
				
				TextView  rplCount = (TextView) item.findViewById(R.id.rplCount);
				rplCount.setText("("+data.get("rn").toString()+")回复");
				
				return item;
			} catch (Exception e) {
				return convertView;
			}
		}
		return convertView;
	}

}
